﻿// 功能：照相机
// 描述：
// 作者：温伟鹏
// 日期：2010/9/2 16:06:05

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace MeizuSDK.COM
{
    /// <summary>
    /// 照相机
    /// </summary>
    public sealed class Camera : IDisposable
    {
        #region 私有变量
        private CameraDeviceInterface device;
        private ICameraDeviceInterface camera;

        /// <summary>
        /// 照相机初始化标志
        /// </summary>
        private bool isInitialized;
        private bool isDisposed;
        private CameraMode mode;
        private BrightnessLevel brightnessLevel;
        private int alphaValue;
        private bool drawPreviewFrame;
        #endregion

        #region 公共属性
        /// <summary>
        /// 获取照相机对焦是否完成
        /// </summary>
        public bool IsAFCompleted
        {
            get
            {
                if (camera == null)
                    return false;
                return camera.IsAFEnd();
            }
        }
        /// <summary>
        /// 获取或设置照片存放路径
        /// </summary>
        public string PhotoFilePath
        {
            get
            {
                if (camera != null)
                {
                    return camera.GetPhotoName();
                }

                return string.Empty;
            }
            set
            {
                if (camera != null)
                {
                    camera.SetPhotoName(value);
                }
            }
        }
        /// <summary>
        /// 获取或设置照片大小
        /// </summary>
        public PhotoSize PhotoSize
        {
            get
            {
                if (camera == null)
                    return PhotoSize.VGA;

                int photoSize = camera.GetPhotoSize();

                return (PhotoSize)photoSize;
            }
            set
            {
                if (camera != null)
                {
                    int photoSize = (int)value;
                    camera.SetPhotoSize(photoSize);
                }
            }
        }
        /// <summary>
        /// 获取或设置拍照模式
        /// </summary>
        public CameraMode Mode
        {
            get
            {
                if (camera == null)
                    return CameraMode.Auto;
                return this.mode;
            }
            set
            {
                if (camera != null)
                {
                    this.mode = value;
                    camera.SetMode((int)value);
                }
            }
        }
        /// <summary>
        /// 获取或设置预览区域的亮度
        /// </summary>
        public BrightnessLevel BrightnessLevel
        {
            get
            {
                if (camera == null)
                    return BrightnessLevel.Level_1;
                return this.brightnessLevel;
            }
            set
            {
                if (camera != null)
                {
                    this.brightnessLevel = value;
                    this.camera.SetBrightness((int)value);
                }
            }
        }
        /// <summary>
        /// 获取或设置预览区域的透明值。取值范围为0到15，共16级。
        /// </summary>
        public int PreviewAreaAlphaValue
        {
            get
            {
                return alphaValue;
            }
            set
            {
                if (!isInitialized)
                    throw new CameraNotInitlizedException();
                if (value < 0)
                    value = 0;
                if (value > 15)
                    value = 15;

                if (camera != null)
                {
                    this.alphaValue = value;

                    camera.SetPreviewAreaAlphaValue(value);
                }
            }
        }
        /// <summary>
        /// 是否绘制预览画面。
        /// 此接口主要可用来控制预览画面在当前窗口的绘制，
        /// 用户可根据自身窗口的一些特性来掌握预览画面的绘制时机
        /// </summary>
        public bool IsDrawPreviewFrame
        {
            get{
                return drawPreviewFrame;
            }
            set{
                if(camera!=null)
                {
                    this.drawPreviewFrame = value;
                    camera.IsDrawPreviewFrame(value);
                }
            }
        }
        #endregion

        #region 构造函数
        /// <summary>
        /// 构造照相机实例
        /// </summary>
        public Camera()
        {
            device = new CameraDeviceInterface();

            camera = (ICameraDeviceInterface)device;
        }
        #endregion

        #region 函数
        /// <summary>
        /// 初始化照相机
        /// </summary>
        /// <param name="hWnd">要显示图像内容的控件或窗口句柄</param>
        /// <returns>true成功；false失败</returns>
        public bool InitDevice(IntPtr hWnd)
        {
            if (camera == null)
                return false;

            bool result = camera.InitDevice(hWnd);

            isInitialized = true;

            return result; 
        }
        /// <summary>
        /// 反初始化照相机
        /// </summary>
        /// <returns>true成功；false失败</returns>
        public bool UnInitDevice()
        {
            if (camera == null || !isInitialized)
                return false;

            bool result = camera.UnInitDevice();

            isInitialized = false;

            return result;
        }
        /// <summary>
        /// 开始预览(初始摄像头时默认预览状态为开)。
        /// 默认的预览画面尺寸为VGA格式。
        /// 可以通过SetPhotoSize函数设置画面大小
        /// </summary>
        /// <returns>true成功；false失败</returns>
        public bool StartPreview()
        {
            if (camera == null)
                return false;

            return camera.StartPreview();
        }
        /// <summary>
        /// 拍照
        /// </summary>
        /// <returns>true成功；false失败</returns>
        public bool TakePhoto()
        {
            if (camera == null)
                return false;

            return camera.TakePhoto();
        }
        /// <summary>
        /// 开始自动对焦
        /// </summary>
        /// <returns>true成功；false失败</returns>
        public bool StartAF()
        {
            if (camera == null)
                return false;

            return camera.StartAF();
        }
        /// <summary>
        /// 停止自动对焦
        /// </summary>
        /// <returns>true成功；false失败</returns>
        public bool StopAF()
        {
            if (camera == null)
                return false;

            return camera.StopAF();
        }
        #endregion

        #region IDisposable 成员
        /// <summary>
        /// 销毁资源
        /// </summary>
        public void Dispose()
        {
            if(!isDisposed)
            {
                if (camera != null && isInitialized)
                {
                    camera.UnInitDevice();
                }

                Marshal.ReleaseComObject(device);
                Marshal.ReleaseComObject(camera);

                device = null;
                camera = null;
            }

            isDisposed = true;
        }

        #endregion
    }
    /// <summary>
    /// 照相机未初始化异常
    /// </summary>
    public class CameraNotInitlizedException:Exception
    {
        public CameraNotInitlizedException()
            :base("照相机未初始化。")
        {

        }
    }
}
